/**
 * Copyright 2011 Joe Choi, All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;

import swt.ui.UIApp;

/**
 * @author Joe Choi
 */
public class KeyboardTest {
	public static void main(String[] args) {
		KeyboardTest test = new KeyboardTest();
		test.run();
	}
	
	public KeyboardTest() {
		Text text;
		Button button;
		MyControl myctl;
		
		app = new UIApp();
		shell = new Shell(app.getDisplay());
		shell.setLayout(new FillLayout());
		
		text = new Text(shell, SWT.NONE);
		myctl = new MyControl(shell);
		button = new Button(shell, SWT.NONE);
		
		text.addListener(SWT.Traverse, new TextLsn());
		text.addListener(SWT.FocusIn, new KeyBoardLsn());
		text.addListener(SWT.FocusOut, new KeyBoardLsn());
		text.addListener(SWT.KeyDown, new TextLsn());
		text.addListener(SWT.KeyUp, new KeyBoardLsn());
		text.addListener(SWT.Selection, new KeyBoardLsn());
		
		button.addListener(SWT.Traverse, new KeyBoardLsn());
		button.addListener(SWT.FocusIn, new KeyBoardLsn());
		button.addListener(SWT.FocusOut, new KeyBoardLsn());
		button.addListener(SWT.KeyDown, new KeyBoardLsn());
		button.addListener(SWT.KeyUp, new KeyBoardLsn());
		button.addListener(SWT.Selection, new KeyBoardLsn());
		
		CanvasLsn cl = new CanvasLsn();
		myctl.addListener(SWT.Traverse, new KeyBoardLsn());
		myctl.addListener(SWT.FocusIn, new KeyBoardLsn());
		myctl.addListener(SWT.FocusOut, new KeyBoardLsn());
		myctl.addListener(SWT.KeyDown, cl);
		myctl.addListener(SWT.Paint, cl);
		myctl.addListener(SWT.KeyUp, new KeyBoardLsn());
		myctl.addListener(SWT.Selection, new KeyBoardLsn());
		
		shell.addListener(SWT.Dispose, new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				System.out.println("Shell dispose");
				app.shutdown();
			}
		});
	}
	
	public void run() {
		shell.open();
		app.run();
	}
	
	private static class KeyBoardLsn implements Listener{
		@Override
		public void handleEvent(Event evt) {
			switch(evt.type) {
				case SWT.Traverse : System.out.println(getWidgetType(evt.widget) + "Traverse"); evt.doit = true; break;
				case SWT.FocusIn : System.out.println(getWidgetType(evt.widget) + "FocusIn"); break;
				case SWT.FocusOut : System.out.println(getWidgetType(evt.widget) + "FocusOut"); break;
				case SWT.KeyDown : System.out.println(getWidgetType(evt.widget) + "KeyDown"); break;
				case SWT.KeyUp : System.out.println(getWidgetType(evt.widget) + "KeyUp"); break;
				case SWT.Selection : System.out.println(getWidgetType(evt.widget) + "Selection"); break;
			}
		}

		private String getWidgetType(Widget widget) {
			if(widget instanceof Button) return("Button");
			if(widget instanceof Text) return("Text");
			return("Unknown");
		}
	}
	
	private static class TextLsn implements Listener {
		@Override
		public void handleEvent(Event evt) {
			switch(evt.type) {
				case SWT.Traverse : handleTraveral(evt); break;
				case SWT.KeyDown : handleKeyDown(evt); break;
			}
		}
		
		private void handleTraveral(Event evt) {
			evt.doit = false;
		}
		
		private void handleKeyDown(Event evt) {
			if(SWT.TAB == evt.keyCode) {
				Text t = ((Text) evt.widget);
				t.append("\t");
			}
		}
	}
	
	private static class CanvasLsn implements Listener {
		@Override
		public void handleEvent(Event evt) {
			switch(evt.type) {
				case SWT.Paint : handlePaint(evt); break;
				case SWT.KeyDown : handleKeyDown(evt); break;
			}
		}
		
		private void handlePaint(Event evt) {
			Rectangle bounds = ((Canvas) evt.widget).getClientArea();
            int top = bounds.y;
            int left = bounds.x;
            
            evt.gc.drawString(t2, top, left);
            System.out.println("Canvas Paint:" + t2 + "," + top + "," + left);
		}
		
		private void handleKeyDown(Event evt) {
            t1 = evt.text;
            t2 = String.valueOf(evt.character);
			System.out.println("Canvas KeyDown:" + t1 + "," + t2);
			
			((Canvas) evt.widget).redraw();
		}
		
		private String t1 = "", t2 ="";
		private int i=0;
	}
	
	private static class MyControl extends Canvas {
		MyControl(Composite parent) {
			super(parent, SWT.BORDER);
		}
	}
	
	private UIApp app;
	private Shell shell;
}
